package com.tag.service.impl;

import com.tag.entity.ApiResult;
import com.tag.entity.model.Tag;
import com.tag.entity.model.TagExample;
import com.tag.entity.param.TagParam;
import com.tag.mapper.TagMapper;
import com.tag.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TageServiceImpl implements TagService {

    @Autowired
    private TagMapper tagMapper;

    @Override
    public ApiResult addTag(Tag param) {
        int affected = tagMapper.insertSelective(param);
        if (affected > 0 ){
            return ApiResult.ok();
        }else{
            return ApiResult.fail();
        }
    }

    @Override
    public ApiResult deleteTagById(Tag param) {
        int affected = tagMapper.deleteByPrimaryKey(param.getId());
        if (affected > 0 ){
            return ApiResult.ok();
        }else{
            return ApiResult.fail();
        }
    }

    @Override
    public ApiResult updateTagById(Tag param) {
        int affected = tagMapper.updateByPrimaryKeySelective(param);
        if (affected > 0 ){
            return ApiResult.ok();
        }else{
            return ApiResult.fail();
        }
    }

    @Override
    public ApiResult selectTagList(TagParam param) {
        TagExample tagExample = new TagExample();
        tagExample.setOrderByClause("create_time desc");
        TagExample.Criteria criteria = tagExample.createCriteria();
        List<Tag> tags = tagMapper.selectByExample(tagExample);
        return ApiResult.ok(tags);
    }
}
